import {Component} from '@angular/core';
import {Events, IonicPage, ModalController, NavController, NavParams} from 'ionic-angular';
import {HttpProvider} from "@providers/http";
import {DialogProvider} from "@providers/dialog";
import {StorageProvider} from "@providers/storage";
import {ManagerProvider} from "@providers/interface/manager";

/**
 * 订单详情页面
 */

@IonicPage()
@Component({
  selector: 'page-manager-order-detail',
  templateUrl: 'manager-order-detail.html',
})
export class ManagerOrderDetailPage {
  param = this.navParams.data;
  ID: any = this.navParams.get("ID");
  data: any = {};
  events: any[] = [];//单据流转信息
  userInfo: any;

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private http: HttpProvider,
              private dialog: DialogProvider,
              private storageService: StorageProvider,
              private modalCtrl: ModalController,
              private event: Events,
              private manager: ManagerProvider) {


    this.userInfo = this.storageService.read('userInfo');
  }

  ionViewDidLoad() {
    this.getOrderDetail();
    this.selectEvent();
  }

  /**
   * 抢单点击事件
   */
  grabOrders() {
    this.dialog.showConfirm(() => {
      this.dialog.showLoading();
      let temp = [];
      temp.push(this.ID);
      this.manager.managerGrabsorder(temp)
        .then(message => {
          this.param.STATUSID = 'GRABED';
          this.dialog.hideLoading();
        })
        .catch(error => {
          this.dialog.showAlert('抢单派工失败！' + error);
          this.dialog.hideLoading();
        });
    }, '是否执行抢单派工操作？');
  }

  //信息审核提交
  //"0":"完工","2":"作废","3":"改派","4":"撤单"
  makeOrderCheck(data) {
    let HZLX: { key: string, value: string };
    if (data == 3) {
      HZLX = {
        "key": "3",
        "value": "改派"
      };
      let a = {};
      a['WD'] = this.param.PROVIDER;
      if (this.param.SERVICETYPEID == 1) {
        a['shlx'] = '1';
      } else {
        a['fwlx'] = '1';
      }
      let modal = this.modalCtrl.create('ManagerMworksPage', a);
      modal.onDidDismiss(value => {
        let RYMC: string, RYBM: string;
        if (value) {
          RYBM = value.KEY;
          RYMC = value.VALUE;
          this.dialog.showLoading();
          this.http.post("/Inbound/invoke.do", {
            interfaceId: "csm.css.jcdy.cssorderjk.makesordercheck",
            data: JSON.stringify({
              "RYMC": RYMC,
              "RYBM": RYBM,
              "ZDRMC": this.userInfo.RYXX02,
              "JDDH": this.param.ID,
              "HZLX": HZLX,
              "RYLX": this.param.SERVICETYPEID
            })
          }).subscribe(message => {
            this.navCtrl.pop();
            this.event.publish('ManagerOrderPage');//通知业务页面刷新
            this.dialog.hideLoading();
          }, error => {
            this.dialog.showAlert('派工失败！' + error);
            this.dialog.hideLoading();
          });
        }
      });
      modal.present();
    } else {
      if (data == 0) {
        HZLX = {
          "key": "0",
          "value": "完工"
        }
      } else if (data == 4) {
        HZLX = {
          "key": "4",
          "value": "撤单"
        }
      } else if (data == 2) {
        HZLX = {
          "key": "2",
          "value": "作废"
        }
      }
      this.dialog.showConfirm(() => {
        this.dialog.showLoading();
        this.http.post("/Inbound/invoke.do", {
          interfaceId: "csm.css.jcdy.cssorderjk.makesordercheck",
          data: JSON.stringify({
            "ZDRMC": this.userInfo.RYXX02,
            "JDDH": this.param.ID,
            "HZLX": HZLX,
            "RYLX": this.param.SERVICETYPEID
          })
        }).subscribe(message => {
          this.navCtrl.pop();
          this.event.publish('ManagerMworksPage');//通知业务页面刷新
          this.dialog.hideLoading();
        }, error => {
          this.dialog.showAlert('操作失败！' + error);
          this.dialog.hideLoading();
        });
      }, '是否确定执行此操作吗？');
    }
  }

  /**
   * 派工
   */
  dispatch() {
    let a = {};
    a['WD'] = this.param.PROVIDER;
    if (this.param.SERVICETYPEID == 1) {
      a['shlx'] = '1';
    } else {
      a['fwlx'] = '1';
    }
    let modal = this.modalCtrl.create('ManagerMworksPage', a);
    modal.onDidDismiss(value => {
      if (value) {
        let temp = {};
        temp['ZCR'] = this.userInfo.RYXX02;
        if (this.param.SERVICETYPEID == 1) {
          temp['XZSHRYDM'] = value.KEY;
          temp['XZSHRY'] = value.VALUE;
        } else {
          temp['FWRYDM'] = value.KEY;
          temp['FWRY'] = value.VALUE;
        }
        temp['LIST'] = [];
        temp['LIST'].push({"GD01": this.param.ID});
        this.dialog.showLoading();
        this.http.post("/Inbound/invoke.do", {
          interfaceId: "csm.css.jcdy.cssorderjk.jx",
          data: JSON.stringify(temp)
        }).subscribe(message => {
          this.navCtrl.pop();
          this.event.publish('ManagerMworksPage');//通知业务页面刷新
          this.dialog.hideLoading();
        }, error => {
          this.dialog.showAlert('派工失败！' + error);
          this.dialog.hideLoading();
        });
      }
    });
    modal.present();
  }

  //获取单据明细
  getOrderDetail() {
    this.http.post("/Inbound/invoke.do", {
      interfaceId: "csm.css.jcdy.qcfwquery.checkxx",
      data: JSON.stringify({"GDDH": this.ID})
    }).subscribe(message => {
      this.data = message.data[0];
    }, error => {
      this.dialog.showAlert('请检查网络！' + error);
    });
  }

  //获取单据流转
  selectEvent() {
    this.http.post("/Inbound/invoke.do", {
      interfaceId: "csm.som.cssOrderquery.selectevent",
      data: JSON.stringify({"ID": this.ID})
    }).subscribe(message => {
      this.events = message.data;
    }, error => {
      this.dialog.showAlert('请检查网络！' + error);
    });
  }
}
